package com.glq1218.user.controller;

import com.glq1218.article.bo.ArticleBO;
import com.glq1218.article.dto.ArticleDTO;
import com.glq1218.domain.*;
import com.glq1218.user.bo.AddUserBO;
import com.glq1218.user.bo.QueryUserBO;
import com.glq1218.user.bo.UpdateUserBO;
import com.glq1218.user.bo.UserBO;
import com.glq1218.user.dto.AddUserDTO;
import com.glq1218.user.dto.QueryUserDTO;
import com.glq1218.user.dto.UpdateUserDTO;
import com.glq1218.user.dto.UserDTO;
import com.glq1218.user.entity.User;
import com.glq1218.user.service.UserService;
import com.glq1218.util.BeanCopyUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author glq1218
 * @description 用户控制器
 * @since 2023/4/10 9:33
 */
@RestController("userController")
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {
    /**
     * 服务对象
     */
    private final UserService userService;

    /**
     * 分页查询用户列表
     *
     * @param paginationParam 分页数据传输对象
     * @param sortParam       排序数据传输对象
     * @param intervalStr     时间间隔数据传输对象
     * @param queryUserDTO    查询用户信息数据传输对象
     * @return 符合条件的所有数据
     */
    @GetMapping
    @PreAuthorize("hasAuthority('system:user:list')")
    public R<PaginationResult<UserDTO>> queryUserList(PaginationParam paginationParam, SortParam sortParam, IntervalStr intervalStr, QueryUserDTO queryUserDTO) {
        // 将DTO转为BO
        QueryUserBO queryUserBO = BeanCopyUtils.copyBean(queryUserDTO, QueryUserBO.class);

        // 调用service获取用户信息
        PaginationResult<UserBO> paginationResult = userService.queryUserList(paginationParam, sortParam, intervalStr, queryUserBO);
        // 获取BO转换为DTO
        List<UserDTO> userDTOList = new ArrayList<>();
        // 结果不为空才能进行转换
        if (Objects.nonNull(paginationResult.getRecords())) {
            List<UserBO> records = paginationResult.getRecords();
            userDTOList = BeanCopyUtils.copyBeanList(records, UserDTO.class);
        }
        // 返回封装好的分页查询DTO
        return R.ok(PaginationResult.<UserDTO>builder()
                .total(paginationResult.getTotal())
                .records(userDTOList)
                .build()
        );
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R<UserDTO> getUserById(@PathVariable Serializable id) {
        UserBO userBO = userService.getUserById(id);
        if (userBO == null) {
            return R.fail("不存在该id的用户信息");
        }
        UserDTO userDTO = BeanCopyUtils.copyBean(userBO, UserDTO.class);
        return R.ok(userDTO);
    }

    /**
     * 新增数据
     *
     * @param addUserDTO 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R<?> addUser(@RequestBody AddUserDTO addUserDTO) {
        AddUserBO addUserBO = BeanCopyUtils.copyBean(addUserDTO, AddUserBO.class);
        String result = userService.addUser(addUserBO);
        return R.ok(result);
    }

    /**
     * 修改数据
     *
     * @param updateUserDTO 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R<?> updateUser(@RequestBody UpdateUserDTO updateUserDTO) {
        UpdateUserBO updateUserBO = BeanCopyUtils.copyBean(updateUserDTO, UpdateUserBO.class);
        String result = userService.updateUser(updateUserBO);
        return R.ok(result);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R<?> deleteByIds(@RequestParam("idList") List<Long> idList) {
        return R.ok(userService.removeByIds(idList));
    }

    @PutMapping("/updateUserInfo")
    public R<?> updateUserInfo(@RequestBody UpdateUserDTO updateUserDTO) {
        User user = BeanCopyUtils.copyBean(updateUserDTO, User.class);
        boolean update = userService.updateById(user);
        if (update) {
            return R.ok("修改成功");
        }
        throw new RuntimeException("修改失败");
    }

    @GetMapping("/queryRentalArticle/{userId}")
    public R<List<ArticleDTO>> queryRentalArticle(@PathVariable Long userId) {
        List<ArticleBO> articleBOList = userService.queryRentalArticle(userId);
        return R.ok(BeanCopyUtils.copyBeanList(articleBOList, ArticleDTO.class));
    }
}
